package com.changgou.search.controller;

import com.changgou.search.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;
import java.util.Set;

//@Controller
@RestController
@RequestMapping("/sku_search")
public class SearchController {
    @Autowired
    private SearchService searchService;
    /**
     * 全文检索
     * @return
     */
    @GetMapping
    public Map search(@RequestParam Map<String,String> paraMap) throws Exception {
        //特殊符号处理
        handlerSearchMap(paraMap);
        Map search = searchService.search(paraMap);
        return search;
    }
    //工具方法处理特殊符号
    private void handlerSearchMap(Map<String, String> paraMap) {
        if (paraMap!=null){
            Set<Map.Entry<String, String>> entries = paraMap.entrySet();
            for (Map.Entry<String, String> entry : entries) {
                if (entry.getKey().startsWith("spec_")){
                    //转移特殊符号
                    String newValue = entry.getValue().replace(" ", "%2B");
                    paraMap.put(entry.getKey(),newValue);
                }
            }
        }
    }
    //搜索页面   http://localhost:9009/search/list?keywords=手机&brand=三星&spec_颜色=粉色&
    //入参：Map
    //返回值 Map
    //由于页面是thymeleaf 完成的 属于服务器内页面渲染 跳转页面
    /*@GetMapping("/list")
    public String search(@RequestParam Map<String,String> searchMap, Model model) throws Exception {
        //特殊符号处理
        handlerSearchMap(searchMap);
        Map resultMap  = searchService.search(searchMap);
        model.addAttribute("searchMap",searchMap);
        model.addAttribute("result",searchMap);
        return "search";
    }*/
}
